<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions 
 * @package    Communityad
 * @copyright  Copyright 2009-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: adboard.tpl 2011-02-16 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
?>
<script type="text/javascript">
	// FUNCTION: Call for show 'Like' and 'Unlike' in the widgets.
  var communityad_likeinfo = function(ad_id, resource_type, resource_id, owner_id, widgetType, core_like) {
    // SENDING REQUEST TO AJAX
    var request = createLike(ad_id, resource_type, resource_id, owner_id, widgetType, core_like);
    // RESPONCE FROM AJAX
    request.addEvent('complete', function(responseJSON) {
      if(responseJSON.like_id )
      {
        $(widgetType + '_likeid_info_'+ ad_id).value = responseJSON.like_id;
        $(resource_type + '_' + widgetType + '_most_likes_' + ad_id).style.display = 'none';
        $(resource_type + '_' + widgetType + '_unlikes_'+ ad_id).style.display = 'block';
      }
      else
      {
        $(widgetType + '_likeid_info_'+ ad_id).value = 0;
        $(resource_type + '_' + widgetType + '_most_likes_' + ad_id).style.display = 'block';
        $(resource_type +'_' + widgetType + '_unlikes_'+ ad_id).style.display = 'none';
      }
    });
	}
</script>

<div class="headline">
	<h2>
		<?php echo $this->translate('Advertising');  ?>
	</h2>
	<div class='tabs'>
		<?php echo $this->navigation($this->navigation)->render() ?>
	</div>
</div>

<div class="layout_middle" id="advertisment_content">
	<div class="cadcomp_vad_header">
		<h3><?php echo $this->translate("Ad Board") ?></h3>
		
		<?php
			$viewer_id = $this->viewer()->getIdentity();
			if( !empty($this->getStoryStatus) && !empty($viewer_id) ) {
			  $communityadURL = $this->url(array(), 'communityad_display', true);
			  $sponcerdURL = $this->url(array(), 'sponcerd_display', true);
			
			  echo '<div class="caab_ad_links"><a href="'. $communityadURL .'" class="caab_ad_links_selected">' . $this->translate($this->getCommunityadTitle) . '</a> <a href="'. $sponcerdURL .'">' . $this->translate('Sponsored Stories') . '</a></div>';
			}
		?>
		
   <?php if(Engine_Api::_()->communityad()->enableCreateLink()) : ?>
		<div class="cmad_hr_link">
			<?php $create_ad_url = $this->url(array(), 'communityad_listpackage', true); ?>
			<a href="<?php echo $create_ad_url; ?>"><?php echo $this->translate("Create an Ad"); ?> &raquo;</a>
		</div>
		<?php endif;?>
	</div>
	<div class="caab_ad">
<?php  if ( empty($this->noResult) ){ ?>
	<?php $div_id = 0;$div_limit = 1;		
		foreach ($this->communityads_array as $community_ad):
			$encode_adId = Engine_Api::_()->communityad()->getDecodeToEncode("".$community_ad['userad_id']."");
			if( !empty($community_ad['resource_type']) && !empty($community_ad['resource_id']) ) {
				$resource_url = Engine_Api::_()->communityad()->resourceUrl( $community_ad['resource_type'], $community_ad['resource_id'] );
			}
		?>
		<?php if($div_limit % 5  == 1){ echo '<div class="caab_list_row caab_list_row_full_aaboard">'; } ?>
			<?php Engine_Api::_()->communityad()->adViewCount( $community_ad['userad_id'], $community_ad['campaign_id'] ); ?>
			<?php //endif; ?>
			<div class="caab_list">
				<!-- DIV: Which show when click on cross of advertisment. -->
				<div id= "ad_board_ad_cancel_<?php echo $div_id; ?>" style="display:none;" class="cmadrem" style="width:150px;">
					<div class="cmadrem_rl">
						<?php echo '<a class="" title="' . $this->translate('Cancel reporting this ad') . '" href="javascript:void(0);" onclick="adUndo('.$div_id.', \'ad_board\');">'. $this->translate('Undo') . '</a>'; ?>
					</div>
					<div class="cmadrem_con">
						<?php echo $this->translate("Do you want to report this? Why didn't you like it?"); ?>
						<form>
						<?php $ads_id = $encode_adId; ?>
						<div><input type="radio" name="adAction" value="0" onclick="adSave('Misleading', '<?php echo $ads_id; ?>', <?php echo $div_id; ?>, 'ad_board')"/><?php echo $this->translate('Misleading'); ?></div>
						<div><input type="radio" name="adAction" value="1" onclick="adSave('Offensive', '<?php echo $ads_id; ?>', <?php echo $div_id; ?>, 'ad_board')"/><?php echo $this->translate('Offensive'); ?></div>
						<div><input type="radio" name="adAction" value="2" onclick="adSave('Inappropriate', '<?php echo $ads_id; ?>', <?php echo $div_id; ?>, 'ad_board')"/><?php echo $this->translate('Inappropriate'); ?></div>
						<div><input type="radio" name="adAction" value="3" onclick="adSave('Licensed Material', '<?php echo $ads_id; ?>', <?php echo $div_id; ?>, 'ad_board')"/><?php echo $this->translate('Licensed Material'); ?></div>
						<div><input type="radio" name="adAction" value="4" onclick="otherAdCannel(4, '<?php echo $div_id; ?>', 'ad_board')" id="ad_board_other_<?php echo $div_id; ?>"/><?php echo $this->translate('Other'); ?></div>
					
						<div><textarea name="ad_board_other_text_<?php echo $div_id; ?>" onclick="this.value=''" onblur="if(this.value=='')this.value='<?php echo $this->translate('Specify your reason here..') ?>';"  id="ad_board_other_text_<?php echo $div_id; ?>" style="display:none;" /><?php echo $this->translate('Specify your reason here..') ?></textarea>
					</div>
					
						<div><?php echo '<a href="javascript:void(0);" onclick="adSave(\'Other\', \''.$ads_id.'\', '.$div_id.', \'ad_board\')" id="ad_board_other_button_'.$div_id.'"  style="display:none" class="cmadrem_button">' . $this->translate('Report') . '</a>'; ?></div>
						</form>
					</div>	
				</div>
			
				<!-- DIV:  Which default show, This div contain the all information about advertisment. -->
				<div class="cmaddis" id="ad_board_ad_<?php echo $div_id; ?>">
					<div class="cmaddis_close">
						<?php $is_identity = $this->viewer()->getIdentity();
									$adcancel_enable = Engine_Api::_()->getApi('settings', 'core')->getSetting('adcancel.enable', 1);
									if(!empty($is_identity) && !empty($adcancel_enable)) { echo	'<a class="" title="' . $this->translate('Report this ad') . '" href="javascript:void(0);" onclick="adCancel('.$div_id.', \'ad_board\');"></a>'; } ?>
					</div>
					<div class="cmad_addis">
						<!--tital code start here for both-->
						<div class="cmaddis_title">
							<?php // Title if has existence on site then "_blank" not work else work.
								if ( !empty($community_ad['resource_type']) && !empty($community_ad['resource_id']) ) {
									$set_target = '';
								} else {
									$set_target = 'target="_blank"';
								}
								echo '<a href="'. $this->url(array('adId' => $encode_adId), 'communityad_adredirect', true)  .'" '.$set_target.' >' . ucfirst($community_ad['cads_title']) . "</a>";
							?>
						</div>
						<?php
							if ( !empty($community_ad['resource_type']) && !empty($community_ad['resource_id']) ) { ?>
								<div class="cmaddis_adinfo">
								<?php
										if( !empty($resource_url['status']) ) {
											echo '<a href="'. $this->url(array('adId' => $encode_adId), 'communityad_adredirect', true)  .'">' . $resource_url['title'] . "</a>";
										}else {
											echo $resource_url['title'];
										} ?>
								</div>
							<?php } else if( !empty($this->hideCustomUrl) ) {
								  $ad_url = Engine_Api::_()->communityad()->adSubTitle( $community_ad['cads_url'] );
								  echo '<div class="cmaddis_adinfo"><a title="'. $community_ad['cads_url'] .'"href="'. $this->url(array('adId' => $encode_adId), 'communityad_adredirect', true) .'" target="_blank" >' . $this->translate(Engine_Api::_()->communityad()->truncation($ad_url, 25)) . "</a></div>";
							}
						?>
						<!--image code start here for both-->
						<?php
							$community_ad_image = $this->itemPhoto($community_ad, '', '');
						?>
						<div class="cmaddis_image">
						<?php
							echo '<a href="'. $this->url(array('adId' => $encode_adId), 'communityad_adredirect', true)  .'" '.$set_target.' >' .  $community_ad_image . "</a>";
						?>
						</div>
						
						<!--description code start here for both-->
						<div class="cmaddis_body">	
							<?php
								echo '<a href="'. $this->url(array('adId' => $encode_adId), 'communityad_adredirect', true) .'" '.$set_target.' >' .  $community_ad['cads_body'] . "</a>";
							?>
						</div>
						<!--description code end here for both-->
					</div>	
					<?php
						// Condition: If Like:1 & Resource Type & Resource Id is available in database then this means that advertisment has existence in community site then we show the 'Like' option of the user.
						$like_id = '';
						$is_module_enabled = Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled($community_ad['resource_type']);
						if( !empty($this->user_id) && !empty($community_ad['like']) && !empty($community_ad['resource_type']) && !empty($community_ad['resource_id']) && !empty($is_module_enabled) && !empty($resource_url) ) {
							$checkResourceType = $resourceType = $community_ad['resource_type'];
							$resource_id = $community_ad['resource_id'];
							$owner_id = $community_ad['owner_id'];
							$ad_id = $community_ad['userad_id'];

							// Queary: Return array of 'My Friend Id' which liked this advertisment.
							//$friendLikeId = Engine_Api::_()->getDbTable('likes', 'communityad')->isFriendLiked( $ad_id );
							$resource_info = Engine_Api::_()->getDbtable('modules', 'communityad')->getModuleInfo($resourceType);
							if (!empty($resource_info)) {
								$checkResourceType = $resource_info['table_name'];
							}
							$like_ids = Engine_Api::_()->communityad()->check_availability( $ad_id );
							$likeAdInfo = Engine_Api::_()->communityad()->likeAdInfo( $resource_id, $resourceType, $ad_id, $like_ids );

							if( empty($likeAdInfo) ) {
								echo '<div class="cmaddis_cont">' . $this->translate('You like this.') . '</div>';
							}else {
								if( empty($likeAdInfo['is_like']) && empty($likeAdInfo['friend_like']) ) {
									if( !empty($likeAdInfo['total_like']) ) {
										$peoplesLike =	$this->translate(array('%s person likes this.', '%s people like this.', $likeAdInfo['total_like']),$this->locale()->toNumber($likeAdInfo['total_like']));		
										$peoplesLike = '<a href="'.$this->url(array('module' => 'communityad', 'controller' => 'display', 'action' => 'adfriendlike', 'resource_type' => $resourceType, 'resource_id' => $resource_id, 'call_status' => 'public', 'ad_id' => $ads_id), 'default', true).'" class="smoothbox">' . $peoplesLike . '</a>';	
										$peoplesLike = '<div class="cmaddis_cont">' . $peoplesLike . '</div>';
										echo $peoplesLike;
									}
								}else if( !empty($likeAdInfo['is_like']) && empty($likeAdInfo['friend_like']) ) {
									if( !empty($likeAdInfo['total_like']) ) {
										$peoplesLike =	$this->translate(array('%s other likes this.', '%s others like this.', $likeAdInfo['total_like']),$this->locale()->toNumber($likeAdInfo['total_like']));
									}else {
										$peoplesLike = '';
									}	
									$peoplesLike = '<a href="'.$this->url(array('module' => 'communityad', 'controller' => 'display', 'action' => 'adfriendlike', 'resource_type' => $resourceType, 'resource_id' => $resource_id, 'call_status' => 'public', 'ad_id' => $ads_id), 'default', true).'" class="smoothbox">' . $peoplesLike . '</a>';
									$show_label = Zend_Registry::get('Zend_Translate')->_('You and %s');
									$show_label = sprintf($show_label, $peoplesLike);
									$show_label = '<div class="cmaddis_cont">' . $show_label . '</div>';
									echo $show_label;
								}else if( empty($likeAdInfo['is_like']) && !empty($likeAdInfo['friend_like']) ) {
									if( !empty($likeAdInfo['total_like']) ) {
										$peoplesLike =	$this->translate(array('%s other likes this.', '%s others like this.', $likeAdInfo['total_like']),$this->locale()->toNumber($likeAdInfo['total_like']));
									}else {
										$peoplesLike = '';
									}
									$peoplesLike = '<a href="'.$this->url(array('module' => 'communityad', 'controller' => 'display', 'action' => 'adfriendlike', 'resource_type' => $resourceType, 'resource_id' => $resource_id, 'call_status' => 'public', 'ad_id' => $ads_id), 'default', true).'" class="smoothbox">' . $peoplesLike . '</a>';
									$show_label = Zend_Registry::get('Zend_Translate')->_('%s %s');
									$show_label = sprintf($show_label, $likeAdInfo['friend_like'], $peoplesLike);
									$show_label = '<div class="cmaddis_cont">' . $show_label . '</div>';
									echo $show_label;
								}else if( !empty($likeAdInfo['is_like']) && !empty($likeAdInfo['friend_like']) ) {
									if( !empty($likeAdInfo['total_like']) ) {
										$peoplesLike =	$this->translate(array('%s other likes this.', '%s others like this.', $likeAdInfo['total_like']),$this->locale()->toNumber($likeAdInfo['total_like']));
									}else {
										$peoplesLike = '';
									}	
									$peoplesLike = '<a href="'.$this->url(array('module' => 'communityad', 'controller' => 'display', 'action' => 'adfriendlike', 'resource_type' => $resourceType, 'resource_id' => $resource_id, 'call_status' => 'public', 'ad_id' => $ads_id), 'default', true).'" class="smoothbox">' . $peoplesLike . '</a>';	
									$show_label = Zend_Registry::get('Zend_Translate')->_('%s %s');
									$show_label = sprintf($show_label, $likeAdInfo['friend_like'], $peoplesLike);
									$show_label = '<div class="cmaddis_cont">' . $show_label . '</div>';
									echo $show_label;
								}
							}
							// Function Calling: Return int or empty, If loggden user does not liked this advertisment then return empty and if loggden user done liked this advertisment then return id of liked from 'core_likes' table.							
							if (!empty($like_ids[0]['like_id'])) {
								$unlike_show = "display:block";
								$like_show = "display:none";
								$like_id = $like_ids[0]['like_id'];
							}
							else {
								$unlike_show = "display:none;";
								$like_show = "display:block;";
								$like_id = 0;
							}
							?>
							<?php if(empty($like_id)) { // Condition: Show 'Like Link' only when loggden user not like this advertisment. else show message that you have liked this advertismant.   ?>
								<div class="cmad_like_button" id= "<?php echo $resourceType ?>_ad_board_unlikes_<?php echo $encode_adId;?>" style ='display:none;' >
									<?php echo '<div class="cmaddis_cont">' .$this->translate('You like this.'). '</div>'; ?>
									<a href = "javascript:void(0);" onclick = "communityad_likeinfo('<?php echo $encode_adId; ?>', '<?php echo $resourceType ?>', '<?php echo $resource_id ?>', '<?php echo $owner_id ?>', 'ad_board', '<?php echo $resource_url['like']; ?>')">
									<i class="like_thumbdown_icon"></i>
									<span><?php echo $this->translate('Unlike') ?></span>
									</a>									
								</div>
								<div class="cmad_like_button" id= "<?php echo $resourceType ?>_ad_board_most_likes_<?php echo $encode_adId;?>" style ='<?php echo $like_show;?>'>
									<a href = "javascript:void(0);" onclick = "communityad_likeinfo('<?php echo $encode_adId; ?>', '<?php echo $resourceType; ?>', '<?php echo $resource_id ?>', '<?php echo $owner_id ?>', 'ad_board', '<?php echo $resource_url['like']; ?>' )">
									<i class="like_thumbup_icon"></i>
									<span><?php echo $this->translate('Like') ?></span>
									</a>
								</div>
								<input type ="hidden" id = "ad_board_likeid_info_<?php echo $encode_adId;?>" value = "<?php echo $like_id; ?>"  />
							<?php }
						}
					?>
					<input type ="hidden" id = "mixinfolike_<?php echo $encode_adId;?>" value = "<?php echo $like_id; ?>"  />
					
				</div>
			</div>	
			<?php $div_id++; ?>
			
			<?php if($div_limit %5 == 0){ echo '</div>'; } ?>
			<?php $div_limit++; ?>
			<?php endforeach; ?>
			<?php }else { ?>

			<div class='tip' style="text-align:center;padding-top:15px;">
				<span style="float:none;">
					<?php echo $this->translate('No advertisements have been created yet.'); ?>
					<?php if (Engine_Api::_()->communityad()->enableCreateLink()): ?>
						<?php echo $this->translate(' Be the first to %1$screate an ad%2$s.', '<a href="'.$this->url(array(), 'communityad_listpackage', true). '">', '</a>'); ?>
					<?php endif; ?>
			 </span>
			</div>
		<?php } ?>
			<div style="clear:both"></div>
		</div>	
	</div>

<style type="text/css">
	.caab_list_row_full_aaboard
	{
		width:<?php echo (Engine_Api::_()->getApi('settings', 'core')->getSetting('ad.block.width', 150)+20)*5;?>px; 
	}
</style>